import Taro, { Component } from '@tarojs/taro'
import { View, Button } from '@tarojs/components'
import './index.css'
import { getLoginURL } from "../../config/config"

export default class Index extends Component {

  componentWillMount () { }

  componentDidMount () { }

  componentWillUnmount () { }

  componentDidShow () { }

  componentDidHide () { }

  config = {
    navigationBarTitleText: '登录'
  }

  state = {
    account: "",
    password: ""
  }

  toLogin() {
    console.log(this.state);
    Taro.showToast({
      title: "登录成功",
      icon: "success"
    });
    setTimeout(() => {
      Taro.navigateTo({
        url: "/pages/home/index"
      });
    }, 2000);
    /*
    fetch(getLoginURL(this.state.account, this.state.password), {
      method: "GET"
    }).then(res => res.json())
    .catch(err => {
      console.log(err);
      Taro.showToast({
        title: "出现错误",
        icon: "none",
      });
    })
    .then(res => {
      if(res.state === "success") {
        Taro.showToast({
          title: "登录成功",
          icon: "success"
        });
        setTimeout(() => {
          Taro.navigateTo({
            url: "/pages/home/index"
          });
        }, 2000);
      } else if(res.state === "warn") {
        Taro.showToast({
          title: "账号不存在",
          icon: "none"
        });
      } else {
        Taro.showToast({
          title: "登录失败",
          icon: "none"
        });
      }
    });
    */
  }

  render () {
    return (
      <View className="login-page">
        <View className="account-input">
          <View style={{marginRight: "3%"}}>账号:</View>
          <Input 
            type='number' 
            placeholder='输入账号' 
            onInput={value => this.setState({account: value.detail.value})}  
          />
        </View>
        <View className="password-input">
          <View style={{marginRight: "3%"}}>密码:</View>
          <Input 
            type='password' 
            password 
            placeholder='输入密码'
            onInput={value => this.setState({password: value.detail.value})}
          />
        </View>
        <View>
          <Button
            type="primary" 
            style={{width: "100px", height: "30px", fontSize: "12px", marginBottom: "40%", marginTop: "5%"}}
            onClick={() => this.toLogin()}
          >
            登录
          </Button>
        </View>
      </View>
    )
  }
}
